package vg.services.data_base_manager.data.graph;

import vg.services.data_base_manager.data.header.AttributeHeader;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Adds collections attributes to class which will extended.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class AttributeItem extends SystemAttributeItem implements Cloneable {
	// Main data
	protected List<Attribute> attributes;
	
//=============================================================================
//-----------------Constructors------------------------------------------------
	public AttributeItem() {
		this.attributes = new ArrayList<Attribute>();
	}
	
	public AttributeItem(List<Attribute> attributes) {
		this.attributes = attributes;
	}
	
	public AttributeItem(AttributeItem item) {
		super(item);
		this.attributes = new ArrayList<Attribute>(item.attributes);
	}	
	
//=============================================================================
//-----------------PUBLIC METHODS----------------------------------------------
	public void addAttributes(Collection<Attribute> attributes) {
        if (attributes == null || attributes.size() == 0)
            return;

        for (Attribute attr : attributes) {
            addAttribute(attr);
        }
    }

    /**
	 * This method adds a attribute in the vertex.
	 */
	public boolean addAttribute(String key, String value) {
		return attributes.add(new Attribute(key, value, AttributeHeader.STRING_ATTRIBUTE_TYPE));
	}
	
	public boolean addAttribute(String key, String value, int type) {
		return attributes.add(new Attribute(key, value, type));
	}
	
	public boolean addAttribute(Attribute attribute) {
		return attributes.add(attribute);
	}

    public boolean addIntAttribute(String key, int value) {
        return attributes.add(new Attribute(key, value));
    }

    public boolean addFloatAttribute(String key, float value) {
        return attributes.add(new Attribute(key, value));
    }

    public boolean addDoubleAttribute(String key, double value) {
        return attributes.add(new Attribute(key, value));
    }

    public boolean addBooleanAttribute(String key, boolean value) {
        return attributes.add(new Attribute(key, value));
    }

    /**
	 * This methods returns all attributes of this vertex.
     *
     * Note: isn't null.
	 */
	public List<Attribute> getAttributes() {
        return attributes;
	}
	
	/**
	 * This method returns attribute with 
	 * name = name of parameter, else null.
	 * @param name - name of attribute.
	 * @return attribute or null.
	 */
	public Attribute getAttribute(String name) {
		Collection<Attribute>list = this.attributes;
		for(Attribute buf : list) {
			if(buf.getName().equals(name))
				return buf;
		}
		return null;
	}
	
	public boolean isEmpty() {
		return attributes.size() == 0;
	}
	
	@Override
	public AttributeItem clone() {
		AttributeItem item = new AttributeItem();
		for (Attribute attr : attributes)
			item.addAttribute(attr.clone());
		
		// super clone
		item.setStorableId(getStorableId());
		return item;
	}
}
